package org.liveboardgames.agricola.control.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import org.liveboardgames.agricola.control.delegates.LoadGameDelegate;
	import org.liveboardgames.agricola.control.delegates.action.AgricolaActionController;
	import org.liveboardgames.agricola.control.events.LoadGameEvent;
	import org.liveboardgames.agricola.model.AgricolaModelLocator;
	import org.liveboardgames.agricola.vo.Game;

	public class LoadGameCommand implements ICommand, IResponder
	{

		public function execute(event:CairngormEvent):void
		{
			var delegate:LoadGameDelegate = new LoadGameDelegate(this);
			var loadGameEvent:LoadGameEvent = LoadGameEvent(event);
			delegate.loadGame(loadGameEvent.gameId);
		}

		public function result(event:Object):void
		{
			var model:AgricolaModelLocator = AgricolaModelLocator.getInstance();
			model.game = ResultEvent(event).result as Game;
			model.actionController = new AgricolaActionController(model.game);
		}

		public function fault(info:Object):void
		{
			var faultEvent:FaultEvent = FaultEvent(info);
			Alert.show("Error when trying to load the game: " + faultEvent.message, "Error");
		}

	}
}